from django.contrib import admin

from goods.models import GoodsCategory, Goods, GoodsSKU, IndexPromotionBanner

from celery_tasks.tasks import generate_static_index_html

from django.core.cache import cache
# Register your models here.

# 将代码进行封装解耦
class BaseAdmin(admin.ModelAdmin):
    '''点击保存时出发异步任务'''
    def save_model(self, request, obj, form, change):
        # obj表示要保存的对象，调用save(),将对象保存到数据库中
        obj.save()

        # celery异步生成静态文件保存到nginx中
        generate_static_index_html.delay()

        # 删除缓存，更新数据，修改了数据库中的数据就需要删除缓存
        cache.delete('index_page_data')



    '''点击删除时触发异步'''

    def delete_model(self, request, obj):

        obj.delete()

        generate_static_index_html.delay()

        cache.delete('index_page_data')



    # 站点管理：自定义站点管理类
class IndexPromotionBannerAdmin(BaseAdmin):

    # '''点击保存时出发异步任务'''
    # def save_model(self, request, obj, form, change):
    #
    #     # obj表示要保存的对象，调用save(),将对象保存到数据库中
    #     obj.save()
    #
    #     # celery异步生成静态文件保存到nginx中
    #     generate_static_index_html.delay()
    #
    # '''点击删除时触发异步'''
    # def delete_model(self, request, obj):
    #
    #     obj.delete()
    #     generate_static_index_html.delay()
    pass


class GoodsCategoryAdmin(BaseAdmin):

    pass


class GoodsAdmin(BaseAdmin):

    pass

# # 主页促销活动
admin.site.register(IndexPromotionBanner)
#
admin.site.register(GoodsCategory)

admin.site.register(Goods)

admin.site.register(GoodsSKU)

